package com.web.controller;


import com.web.entity.HealthKnow;
import com.web.entity.Result;
import com.web.service.HealthKnowService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/healthKnow")
@CrossOrigin
public class HealthKnowController {

    @Autowired
    private HealthKnowService healthKnowService;

    //增加健康知识{"title": "黄帝内经","content": "读书看报多睡觉","category": "疾病预防","adminId": 3}
    @PostMapping("/add")
    public Result add(@RequestBody HealthKnow healthKnow){
        log.info("增加健康知识：{}",healthKnow);
        healthKnowService.add(healthKnow);
        return Result.success();
    }


    //根据id删除健康知识http://localhost:8080/hn/delete/7
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable int id){
        log.info("根据id删除健康知识：{}",id);
        healthKnowService.deleteById(id);
        return Result.success();
    }


    //根据id修改健康知识http://localhost:8080/hn/update/6
    //{"title": "母猪产前护理","content": "多吃少动","category": "疾病预防","adminId": 1}
    @PutMapping("/update/{id}")
    public Result update(@PathVariable int id,@RequestBody HealthKnow healthKnow){
        log.info("根据id修改健康知识：{}",healthKnow);
        healthKnow.setId(id);
        healthKnowService.update(healthKnow);
        return Result.success();
    }


    //根据id查询健康知识
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable int id){
        log.info("根据id查询健康知识：{}",id);
        HealthKnow healthKnow = healthKnowService.findById(id);
        return Result.success(healthKnow);
    }

    //查询所有健康知识http://localhost:8080/healthKnow/findAll
    @GetMapping("/findAll")
    public Result findAll(){
        log.info("查询所有健康知识");
        List<HealthKnow> healthKnowList = healthKnowService.findAll();

        return Result.success(healthKnowList);
    }
    @GetMapping("/findByName/{name}")
    public Result findByName(@PathVariable String name){
        List<HealthKnow> healthKnowList = healthKnowService.findByName(name);
        return Result.success(healthKnowList);
    }
}